package com.im.tcp.config;

import com.lld.im.common.config.ImTcpConfig;
import com.rabbitmq.client.ConnectionFactory;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ImTpcBootStartConfig {


    /* RabbitProperties spring提供的一个rabbit属性类 自动加载配置文件中的配置信息 */
    /* 选择手动注入 是为了方便管理channel 重复利用 */
    @Bean
    public ConnectionFactory connectionFactory(RabbitProperties rabbitProperties) {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUsername(rabbitProperties.getUsername());
        connectionFactory.setPassword(rabbitProperties.getPassword());
        connectionFactory.setHost(rabbitProperties.getHost());
        connectionFactory.setVirtualHost(rabbitProperties.getVirtualHost());
        connectionFactory.setPort(rabbitProperties.getPort());
        return connectionFactory;
    }

    @Bean
    public RedissonClient redissonClient(ImTcpConfig tcpConfigEntity){
        Config config = new Config();
        config.useSingleServer()
                .setAddress("redis://" + tcpConfigEntity.getRedisConfig().getHost() +
                        ":" + tcpConfigEntity.getRedisConfig().getPort());
        /* 如果使用jackson 进行序列化 会导致字符二次序列化 出现多余的转义字符或者引好 */
        config.setCodec(new StringCodec());
        return Redisson.create(config);
    }
}
